package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.ItemCatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class ItemCatServiceImpl implements ItemCatService {

    private static final Logger logger = LoggerFactory.getLogger(ItemCatServiceImpl.class);

    @Resource
    private TbItemCatMapper tbItemCatMapper;

    @Resource
    private TbItemMapper tbItemMapper;

    @Override
    public PageInfo<TbItemCat> getListItemCat(Integer pageNumber, Integer pageSize, Long parentId) {
        PageHelper.startPage(pageNumber,pageSize);
        TbItemCatExample ice = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = ice.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(ice);
        PageInfo<TbItemCat> pageInfo = new PageInfo<>(tbItemCats);
        logger.info("执行成功");
        return pageInfo;
    }

    @Override
    public TbItemCat getItemById(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertItemCat(TbItemCat tbItemCat) {
        return tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public TbItemCat getTbItemCatById(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateTbItemCat(TbItemCat tbItemCat) {
        return tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    public int deleteItemCat(Long[] ids) {
        return tbItemCatMapper.deleteItemCat(ids);
    }

    @Override
    public List<TbItemCat> getItemByIds(Long[] ids) {
        List<TbItemCat> itemCats = new ArrayList<>();
        for (Long id : ids){
            TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
            itemCats.add(tbItemCat);
        }
        logger.info("执行成功");
        return itemCats;
    }

    @Override
    public List<TbItemCat> getItemByParentId(Long parentId) {
        TbItemCatExample ice = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = ice.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(ice);
        logger.info("执行成功");
        return tbItemCats;
    }

    @Override
    public List<TbItem> getItemByGoodsId(Long goodsId) {
        TbItemExample tie = new TbItemExample();
        TbItemExample.Criteria criteria = tie.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andStatusEqualTo("1");

        //按照default排序
        tie.setOrderByClause(" is_default desc ");
        logger.info("执行成功");
        return tbItemMapper.selectByExample(tie);
    }

    @Override
    public TbItem getItemByItemId(Long itemId) {
        return tbItemMapper.selectByPrimaryKey(itemId);
    }
}
